package threading;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xwl
 * Date: 2023-06-14
 * Time: 7:49
 */
public class ThreadDemo6 {
    public static void main(String[] args) {
        Thread t = new Thread( () -> {
            // currentThread -- 获取当前线程实例，t
            // isInterrupted -- t对象自带的一个标志位,默认为false
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("hello t");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    //
                    break;
                }
            }
        });
        //
        t.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        // 1.设置标志位为true
        // 2.如果该线程正在阻塞中,就会把阻塞状态唤醒,通过抛异常的方式让sleep立即结束,并会把标志位清空(true -> false)
        t.interrupt();

    }
}